package com.sinopec.handdevice.api;

import com.sinopec.basemodule.BaseApplication;
import com.sinopec.basemodule.http.HttpParam;
import com.sinopec.basemodule.http.HttpParamObj;
import com.sinopec.basemodule.http.api.AppService;
import com.sinopec.basemodule.http.api.HttpResult;
import com.sinopec.basemodule.http.api.LoginService;
import com.sinopec.basemodule.http.api.RetroAPIFactory;
import com.sinopec.basemodule.model.login_model.PasswordPrepareModel;
import com.sinopec.basemodule.model.UserInfo;

import java.util.HashMap;

import io.reactivex.Observable;

/**
 * Created by ronny on 2018/3/27.
 */

public class LoginApi {
    static {
        mLoginApi = new LoginApi();
    }

    private static LoginApi mLoginApi;
    private final LoginService mineService;
    private final AppService apiService;

    public LoginApi() {
        apiService = RetroAPIFactory.create(AppService.class);
        mineService = RetroAPIFactory.create(LoginService.class);
    }

    public static LoginApi getInstance() {
        return mLoginApi;
    }


    public Observable<HttpResult<UserInfo>> login(String userPhone, String password, String deviceNo) {
        HashMap<String, String> paramMap = HttpParam.createParams(true)
                .putParam("userAccount", userPhone)
                .putParam("password", password)
                .putParam("equipmentCode", deviceNo)//设备号
                .end();
        return mineService.login(paramMap);
    }

    /**
     * 登录获取验证码
     */
    public Observable<HttpResult<String>> getCode(String mobilePhone) {
        HashMap<String, String> paramMap = HttpParam.createParams(true)
                .putParam("mobile", mobilePhone)//手机号
                .putParam("platform", "2")//平台 1是IOS,2是ANDROID
                .putParam("type", "2")//验证码类型 1、注册 2登陆 3找回密码
                .putParam("version", BaseApplication.getInstance().getVersionName())//版本号
                .end();
        return mineService.getCode(paramMap);
    }


    /**
     * 获取验证码
     *
     * @param platform 平台 1是IOS,2是ANDROID
     * @param type     验证码类型 1登陆 2找回密码
     */
    public Observable<HttpResult<String>> getVerificationCode(String newPhoneNumberStr, int platform, int type) {
        HashMap<String, Object> end = HttpParamObj.createParams(true)
                .putParam("mobile", newPhoneNumberStr)
                .putParam("platform", platform)//平台 1是IOS,2是ANDROID
                .putParam("type", type)//验证码类型 1登陆 2找回密码
                .putParam("version", BaseApplication.getInstance().getVersionName())//版本号
                .end();
        return mineService.getVerificationCode(end);
    }

    /**
     * 找回密码，下一步
     */
    public Observable<HttpResult<PasswordPrepareModel>> findPasswordPrepare(String phoneNumberStr, String smsStr) {
        HashMap<String, Object> end = HttpParamObj.createParams(true)
                .putParam("userAccount", phoneNumberStr)//用户账号
                .putParam("verificationCode", smsStr)//手机验证码
                .end();
        return mineService.findPasswordPrepare(end);
    }

    /**
     * 找回密码
     */
    public Observable<HttpResult<String>> findPassword(String updatePasswordToken, String passwordStr) {
        HashMap<String, Object> end = HttpParamObj.createParams(true)
                .putParam("password", passwordStr)//密码
//                .putParam("userAccount", phoneNumberStr)//用户账号
//                .putParam("verificationCode", smsStr)//手机验证码
                .putParam("updatePasswordToken", updatePasswordToken)//验证Token
                .end();
        return mineService.findPassword(end);
    }
}
